/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bhims;

import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlImageInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.awt.List;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Rein
 */
public class TwitterFunctions {

    boolean TwitterLogIn(WebClient wc, String accountString) {
        boolean success = false;
        String[] accountdetailsString = accountString.split(":");
        String Username, Password, IP, ProxyUsername, ProxyPassword;
        int Port;
        if (accountdetailsString.length == 2) {
            Username = accountdetailsString[0];
            Password = accountdetailsString[1];
        } else if (accountdetailsString.length == 4) {
            Username = accountdetailsString[0];
            Password = accountdetailsString[1];
            IP = accountdetailsString[2];
            Port = Integer.valueOf(accountdetailsString[3]);
            wc.getOptions().getProxyConfig().setProxyHost(IP);
            wc.getOptions().getProxyConfig().setProxyPort(Port);
        } else if (accountdetailsString.length == 6) {
            Username = accountdetailsString[0];
            Password = accountdetailsString[1];
            IP = accountdetailsString[2];
            Port = Integer.valueOf(accountdetailsString[3]);
            ProxyUsername = accountdetailsString[4];
            ProxyPassword = accountdetailsString[5];

            wc.getOptions().getProxyConfig().setProxyHost(IP);
            wc.getOptions().getProxyConfig().setProxyPort(Port);
            DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) wc.getCredentialsProvider();
            credentialsProvider.addCredentials(ProxyUsername, ProxyPassword);
        } else {
            return false;
        }




        try {
            HtmlPage LogInPage = wc.getPage("https://mobile.twitter.com/session/new");


            HtmlTextInput UsernameTextInput = LogInPage.getElementByName("username");
            UsernameTextInput.setValueAttribute(Username);

            HtmlPasswordInput passwordInput = LogInPage.getElementByName("password");
            passwordInput.setValueAttribute(Password);

            HtmlSubmitInput OkButtonInput = LogInPage.getElementByName("commit");

            HtmlPage ResultPage = OkButtonInput.click();
            if (ResultPage.asText().contains("Settings")) {
                return true;
            }

        } catch (IOException ex) {
            return false;
        } catch (FailingHttpStatusCodeException ex) {
            return false;
        }


        return success;
    }

    ArrayList ScrapeUsersBySearch(WebClient wc, String keyword, int Ratio) {
        ArrayList UsersScrapedList = new ArrayList();

        try {

            HtmlPage MainPage;
            MainPage = wc.getPage("https://mobile.twitter.com/");
            HtmlTextInput SearchBoxInput = MainPage.getElementByName("q");
            SearchBoxInput.setValueAttribute(keyword);
            HtmlImageInput SearchButtonImageInput = (HtmlImageInput) MainPage.getByXPath("//*[@alt=\"Search\"]").get(0);
            HtmlPage ResultPage = (HtmlPage) SearchButtonImageInput.click();


            do {

                ArrayList UserHtmlList;
                UserHtmlList = (ArrayList) ResultPage.getByXPath("//*[@class=\"username\"]");

                for (int i = 0; i < UserHtmlList.size(); i++) {
                    HtmlSpan object = (HtmlSpan) UserHtmlList.get(i);
                    UsersScrapedList.add(object.getTextContent().trim());
                    if (UserHtmlList.size() >= Ratio) {
                        break;
                    }
                }

                if (ResultPage.asText().contains("Load older Tweets")) {
                    HtmlAnchor NextPageAnchor = ResultPage.getAnchorByText("Load older Tweets");
                    ResultPage = NextPageAnchor.click();

                } else {
                    break;
                }

            } while (UsersScrapedList.size() < Ratio);




        } catch (IOException ex) {
            return UsersScrapedList;
        } catch (FailingHttpStatusCodeException ex) {
            return UsersScrapedList;
        }

        return UsersScrapedList;
    }

    ArrayList ScrapeUserFollowers(WebClient wc, String user) {
        ArrayList UsersScrapedList = new ArrayList();
        try {

            HtmlPage MainPage;
            MainPage = wc.getPage("https://mobile.twitter.com/" + user + "/followers");

            do {

                ArrayList UserHtmlList;
                UserHtmlList = (ArrayList) MainPage.getByXPath("//*[@class=\"username\"]");

                for (int i = 0; i < UserHtmlList.size(); i++) {
                    HtmlSpan object = (HtmlSpan) UserHtmlList.get(i);
                    UsersScrapedList.add(object.getTextContent().trim());
                    System.out.println(object.getTextContent().trim());
                }

                if (MainPage.asText().contains("Show more people")) {
                    HtmlAnchor NextPageAnchor = MainPage.getAnchorByText("Show more people");
                    MainPage = NextPageAnchor.click();

                } else {
                    break;
                }

            } while (true);




        } catch (IOException ex) {
            return UsersScrapedList;
        } catch (FailingHttpStatusCodeException ex) {
            return UsersScrapedList;
        }
        return UsersScrapedList;
    }

    ArrayList ScrapeUserFollowing(WebClient wc, String user) {
        ArrayList UsersScrapedList = new ArrayList();
        try {

            HtmlPage MainPage;
            MainPage = wc.getPage("https://mobile.twitter.com/" + user + "/following");

            do {

                ArrayList UserHtmlList;
                UserHtmlList = (ArrayList) MainPage.getByXPath("//*[@class=\"username\"]");

                for (int i = 0; i < UserHtmlList.size(); i++) {
                    HtmlSpan object = (HtmlSpan) UserHtmlList.get(i);
                    UsersScrapedList.add(object.getTextContent().trim());
                    System.out.println(object.getTextContent().trim());
                }

                if (MainPage.asText().contains("Show more people")) {
                    HtmlAnchor NextPageAnchor = MainPage.getAnchorByText("Show more people");
                    MainPage = NextPageAnchor.click();

                } else {
                    break;
                }

            } while (true);




        } catch (IOException ex) {
            return UsersScrapedList;
        } catch (FailingHttpStatusCodeException ex) {
            return UsersScrapedList;
        }
        return UsersScrapedList;
    }

    boolean FollowUser(WebClient wc, String User) {
        try {
            HtmlPage ProfilePage = wc.getPage("https://mobile.twitter.com/" + User + "?p=s");
            HtmlSubmitInput FollowSubmitInput = ProfilePage.getElementByName("commit");
            if (!"Follow".equals(FollowSubmitInput.getValueAttribute())) {
                return false;
            }

            HtmlPage ReturnedPage = FollowSubmitInput.click();
            HtmlSubmitInput UnFollowSubmitInput = ReturnedPage.getElementByName("commit");
            if ("Following".equals(UnFollowSubmitInput.getValueAttribute())) {
                return true;
            }


        } catch (IOException ex) {
            return false;
        } catch (FailingHttpStatusCodeException ex) {
            return false;
        }
        return false;
    }

    boolean UnFollowUser(WebClient wc, String User) {
        try {
            HtmlPage ProfilePage = wc.getPage("https://mobile.twitter.com/" + User + "?p=s");
            HtmlSubmitInput FollowSubmitInput = ProfilePage.getElementByName("commit");
            if (!"Following".equals(FollowSubmitInput.getValueAttribute())) {
                return false;
            }

            HtmlPage ReturnedPage = FollowSubmitInput.click();
            HtmlSubmitInput UnFollowSubmitInput = ReturnedPage.getElementByName("commit");
            UnFollowSubmitInput.click();
            
            ProfilePage = wc.getPage("https://mobile.twitter.com/" + User + "?p=s");
            FollowSubmitInput = ProfilePage.getElementByName("commit");
            if (!"Follow".equals(FollowSubmitInput.getValueAttribute())) {
                return false;
            }
            
            return true;


        } catch (IOException ex) {
            return false;
        } catch (FailingHttpStatusCodeException ex) {
            return false;
        }
        
    }
}
